<?php

include './core/rongyun.php';

class ChatAction extends  Action{
	//事例化聊天类对象
	private  $p ;
	public function _initialize(){
		$this->p = new ServerAPI(C("__CHATAPP__"),C("__CHATSECTET__"));
	}
	
	//获取token
	public function getToken(){
		$uid = t($_REQUEST["uid"]);
		$uname = t($_REQUEST["uname"]);
		$avatar = t($_REQUEST["avatar"]);
		$r = $this->p->getToken($uid,$uname,$avatar);
		$result = json_decode($r,true);
		//dump($result);exit;
		if($result["code"] == "200"){
			//获取成功
			$token = $result["token"];
			//更新用户token信息
			$user = M("User");
			$user -> chat_token = $token;
			if ($user->where("uid = $uid")->save()){
				$value = ["code" =>"success","token"=>$token];
				header("Content-Type: text/json");
				die(json_encode($value));
			}else{
				$value = ["code" =>"wrong","massage"=>"添加失败"];
				header("Content-Type: text/json");
				die(json_encode($value));
			}
		}else{
			$value = ["code" =>"wrong","massage"=>$result["errorMessage"]];
			header("Content-Type: text/json");
			die(json_encode($value));
		}
		
	}
}